﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;

namespace YJ.Data.MySql
{
    public class UserShortcut : YJ.Data.Interface.IUserShortcut
    {
        private DBHelper dbHelper = new DBHelper();
        /// <summary>
        /// 构造函数
        /// </summary>
        public UserShortcut()
        {
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.UserShortcut实体类</param>
        /// <returns>操作所影响的行数</returns>
        public int Add(YJ.Data.Model.UserShortcut model)
        {
            string sql = @"INSERT INTO usershortcut
				(ID,MenuID,UserID,Sort) 
				VALUES(@ID,@MenuID,@UserID,@Sort)";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = model.ID },
				new MySqlParameter("@MenuID", MySqlDbType.VarChar, 36){ Value = model.MenuID },
				new MySqlParameter("@UserID", MySqlDbType.VarChar, 36){ Value = model.UserID },
				new MySqlParameter("@Sort", MySqlDbType.Int32, 11){ Value = model.Sort }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.UserShortcut实体类</param>
        public int Update(YJ.Data.Model.UserShortcut model)
        {
            string sql = @"UPDATE usershortcut SET 
				MenuID=@MenuID,UserID=@UserID,Sort=@Sort
				WHERE ID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@MenuID", MySqlDbType.VarChar, 36){ Value = model.MenuID },
				new MySqlParameter("@UserID", MySqlDbType.VarChar, 36){ Value = model.UserID },
				new MySqlParameter("@Sort", MySqlDbType.Int32, 11){ Value = model.Sort },
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = model.ID }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(Guid id)
        {
            string sql = "DELETE FROM usershortcut WHERE ID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = id.ToString() }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 将DataRedar转换为List
        /// </summary>
        private List<YJ.Data.Model.UserShortcut> DataReaderToList(MySqlDataReader dataReader)
        {
            List<YJ.Data.Model.UserShortcut> List = new List<YJ.Data.Model.UserShortcut>();
            YJ.Data.Model.UserShortcut model = null;
            while (dataReader.Read())
            {
                model = new YJ.Data.Model.UserShortcut();
                model.ID = dataReader.GetString(0).ToGuid();
                model.MenuID = dataReader.GetString(1).ToGuid();
                model.UserID = dataReader.GetString(2).ToGuid();
                model.Sort = dataReader.GetInt32(3);
                List.Add(model);
            }
            return List;
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.UserShortcut> GetAll()
        {
            string sql = "SELECT * FROM usershortcut";
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.UserShortcut> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询记录数
        /// </summary>
        public long GetCount()
        {
            string sql = "SELECT COUNT(*) FROM usershortcut";
            long count;
            return long.TryParse(dbHelper.GetFieldValue(sql), out count) ? count : 0;
        }
        /// <summary>
        /// 根据主键查询一条记录
        /// </summary>
        public YJ.Data.Model.UserShortcut Get(Guid id)
        {
            string sql = "SELECT * FROM usershortcut WHERE ID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = id.ToString() }
			};
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.UserShortcut> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        public int DeleteByUserID(Guid userID)
        {
            string sql = "DELETE FROM UserShortcut WHERE UserID=@UserID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@UserID", MySqlDbType.VarChar){ Value = userID.ToString() }
			};
            return dbHelper.Execute(sql, parameters);
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.UserShortcut> GetAllByUserID(Guid userID)
        {
            string sql = "SELECT * FROM UserShortcut WHERE UserID=@UserID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@UserID", MySqlDbType.VarChar){ Value = userID.ToString() }
			};
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.UserShortcut> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        public DataTable GetDataTableByUserID(Guid userID)
        {
            string sql = "SELECT * FROM UserShortcut WHERE UserID=@UserID ORDER BY Sort";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@UserID", MySqlDbType.VarChar){ Value = userID.ToString() }
			};
            return dbHelper.GetDataTable(sql, parameters);
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        public int DeleteByMenuID(Guid menuID)
        {
            string sql = "DELETE FROM UserShortcut WHERE MenuID=@MenuID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@MenuID", MySqlDbType.VarChar){ Value = menuID.ToString() }
			};
            return dbHelper.Execute(sql, parameters);
        }
    }
}